package com.v3admin.system.controller;

import com.v3admin.common.controller.IResponse;
import com.v3admin.system.domain.SystemMenu;
import com.v3admin.system.service.SystemMenuService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 系统菜单
 *
 * @author Junpeng.Li
 * @date 2023-12-15 17:22
 */
@RestController
@RequestMapping(value = "/menu")
@AllArgsConstructor
public class SystemMenuController {

    private final SystemMenuService menuService;

    /**
     * 获取所有菜单
     *
     * @return 菜单的集合
     */
    @GetMapping(value = "/list")
    public IResponse findAll() {
        List<SystemMenu> menus = menuService.list();
        menus = menuService.toTree(menus);
        return IResponse.success(menus);
    }

    /**
     * 添加菜单
     *
     * @param menu 菜单信息
     * @return 操作结果
     */
    @PostMapping
    public IResponse add(@RequestBody SystemMenu menu) {
        menuService.save(menu);
        return IResponse.success("菜单添加成功");
    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单ID
     * @return 操作结果
     */
    @DeleteMapping
    public IResponse deleteById(@NotNull(message = "菜单ID不能为空") Long menuId) {
        menuService.removeById(menuId);
        return IResponse.success("菜单删除成功");
    }
}
